package DB;

import Exceptions.DaoException;
import Interfaces.ILogin_Dao;
import Models.Login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Check_UserLOGIN_DB implements ILogin_Dao {

    @Override
    public Login ValidateLogin(String Name, String Pass) throws DaoException {
        Login r = new Login();
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pst = null;
        try {
            conn = javaconnect.ConnecrDb("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/nlr");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Check_UserLOGIN_DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        
         String sql = "select * from  user where username=? and pass=? "; // Το table , username, password ειναι ονομασιες που εδωσα στη βαση μου
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, Name);
            pst.setString(2, Pass);
           
            
            rs = pst.executeQuery();
            if (rs.next()) {
           
            r.setName(rs.getString("username"));
            r.setPass(rs.getString("pass"));
                //TODO put code to transfer to next Frame
            }
              
        
            
        } catch (SQLException e) {
           return null;
        }
        
        
        
       

        return r;
    }

}
